/*
* Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id: connectdialog.cpp 391 2009-05-06 21:06:11Z jpnurmi $
*/

#include "connectdialog.h"
#include <QtGui>

ConnectDialog::ConnectDialog(QWidget* parent) : QDialog(parent), modifying(false)
{
    ui.setupUi(this);

    QList<Connection> conns = Connection::connections();
    foreach (const Connection& connection, conns)
        ui.comboBoxName->addItem(connection.name);

    ui.comboBoxName->setCurrentIndex(-1);
    connect(ui.comboBoxName, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));

    connect(ui.comboBoxName, SIGNAL(editTextChanged(QString)), this, SLOT(validate()));
    connect(ui.comboBoxName, SIGNAL(currentIndexChanged(int)), this, SLOT(validate()));
    connect(ui.lineEditHost, SIGNAL(textChanged(QString)), this, SLOT(validate()));
    connect(ui.lineEditNick, SIGNAL(textChanged(QString)), this, SLOT(validate()));
    connect(ui.spinBoxPort, SIGNAL(valueChanged(int)), this, SLOT(validate()));

    validate();

#ifdef QT_NO_OPENSSL
    ui.secureCheckBox->setEnabled(false);
#endif // QT_NO_OPENSSL
}

Connection ConnectDialog::connection() const
{
    Connection conn;
    conn.name = ui.comboBoxName->currentText();
    conn.host = ui.lineEditHost->text();
    conn.port = ui.spinBoxPort->text().toInt();
    conn.nick = ui.lineEditNick->text();
    conn.real = ui.lineEditReal->text();
    conn.pass = ui.lineEditPass->text();
    conn.autoConnect = ui.autoConnectCheckBox->isChecked();
    conn.secure = ui.secureCheckBox->isChecked();
    return conn;
}

void ConnectDialog::setConnection(const Connection& connection)
{
    ui.comboBoxName->setEditText(connection.name);
    ui.lineEditHost->setText(connection.host);
    ui.spinBoxPort->setValue(connection.port);
    ui.lineEditNick->setText(connection.nick);
    ui.lineEditReal->setText(connection.real);
    ui.lineEditPass->setText(connection.pass);
    ui.autoConnectCheckBox->setChecked(connection.autoConnect);
    ui.secureCheckBox->setChecked(connection.secure);
}

bool ConnectDialog::isModifying() const
{
    return modifying;
}

void ConnectDialog::setModifying(bool modifying)
{
    this->modifying = modifying;
}

void ConnectDialog::accept()
{
    Connection conn = connection();
    if (!modifying && ui.comboBoxName->lineEdit()->isModified() && !conn.name.isEmpty())
    {
        ui.comboBoxName->blockSignals(true);
        QMetaObject::invokeMethod(ui.comboBoxName, "_q_returnPressed");
        ui.comboBoxName->blockSignals(false);
        Connection::save(conn, ui.comboBoxName->currentIndex());
    }
    QDialog::accept();
}

void ConnectDialog::onCurrentIndexChanged(int index)
{
    setConnection(Connection::load(index));
}

void ConnectDialog::validate()
{
    const bool hasName = !ui.comboBoxName->currentText().isEmpty();
    const bool hasHost = !ui.lineEditHost->text().isEmpty();
    const bool hasNick = !ui.lineEditNick->text().isEmpty();
    const bool hasPort = ui.spinBoxPort->hasAcceptableInput();

    ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(hasHost && hasNick && hasPort);
    ui.autoConnectCheckBox->setEnabled(hasName);
}
